package com.zhaopin.qa.jmeter.plugins.jsonpathbatchassertion.gui;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jorphan.gui.ObjectTableModel;
import org.apache.jorphan.reflect.Functor;

import com.zhaopin.qa.jmeter.plugins.jsonpathbatchassertion.JSONPathAssertionArgument;

import sun.swing.table.DefaultTableCellHeaderRenderer;

public class JsonPathAssetionArgumentsPanel extends ArgumentsPanel {

	private static final long serialVersionUID = 240L;

	private static final String JsonPath = "JsonPath"; // 是否只对比JsonPath

	private static final String ExpectValue = "预期值"; // 是否只对比JsonPath

	private static final String JsonValOrNot = "验证值?"; // 是否只对比JsonPath

	private static final String RegularOrNot = "正则匹配?"; // JsonValue对比是否支持正则匹配

	private static final String ExpectNullOrNot = "预期null或不存在?"; // JsonValue值是否为空

	private static final String InvertAssertionOrNot = "反转断言?"; // will fail if above conditions met

	public JsonPathAssetionArgumentsPanel() {
		super("JSON Path 断言列表");
		super.getTable().getTableHeader().setDefaultRenderer(new DefaultTableCellHeaderRenderer());
		super.getTable().setAutoCreateRowSorter(true);
	}

	public static boolean testFunctors() {
		JsonPathAssetionArgumentsPanel instance = new JsonPathAssetionArgumentsPanel();
		instance.initializeTableModel();
		return instance.tableModel.checkFunctors((Object) null, instance.getClass());
	}

	@Override
	protected void initializeTableModel() {
		tableModel = new ObjectTableModel(
				new String[] {
						JsonPath, ExpectValue, JsonValOrNot, RegularOrNot, ExpectNullOrNot, InvertAssertionOrNot },
				JSONPathAssertionArgument.class, // $NON-NLS-2$
				new Functor[] { // $NON-NLS-2$
						new Functor("getJsonPath"), //$NON-NLS-1$
						new Functor("getExpectedValue"), //$NON-NLS-1$
						new Functor("isJsonValidationBool"), //$NON-NLS-1$
						new Functor("isUseRegex"), // $NON-NLS-2$
						new Functor("isExpectNull"), // $NON-NLS-2$
						new Functor("isInvert") }, //$NON-NLS-1$ },
				new Functor[] { // $NON-NLS-2$
						new Functor("setJsonPath"), //$NON-NLS-1$
						new Functor("setExpectedValue"), //$NON-NLS-1$
						new Functor("setJsonValidationBool"), //$NON-NLS-1$
						new Functor("setIsRegex"), // $NON-NLS-2$
						new Functor("setExpectNull"), // $NON-NLS-2$
						new Functor("setInvert") }, //$NON-NLS-1$ },
				new Class[] { String.class, String.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class });

	}

	@Override
	protected JSONPathAssertionArgument makeNewArgument() {
		return new JSONPathAssertionArgument("", "", true, false, false, false);
	}

	@Override
	protected Argument createArgumentFromClipboard(String[] clipboardCols) {
		JSONPathAssertionArgument argument = makeNewArgument();
		switch (clipboardCols.length) {
		default:
		case 6:
			Boolean invert = BooleanUtils.toBooleanObject(clipboardCols[5].trim());
			argument.setInvert(invert != null ? invert.booleanValue() : false);
		case 5:
			Boolean expectnull = BooleanUtils.toBooleanObject(clipboardCols[4].trim());
			argument.setExpectNull(expectnull != null ? expectnull.booleanValue() : false);
		case 4:
			Boolean regular = BooleanUtils.toBooleanObject(clipboardCols[3].trim());
			argument.setIsRegex(regular != null ? regular.booleanValue() : false);
		case 3:
			Boolean jsonval = BooleanUtils.toBooleanObject(clipboardCols[2].trim());
			argument.setJsonValidationBool(jsonval != null ? jsonval.booleanValue() : true);
		case 2:
			argument.setExpectedValue(clipboardCols[1]);
		case 1:
			argument.setJsonPath(clipboardCols[0]);
			return argument;
		}
	}

}
